//index.js
//获取应用实例
const api = require('../../utils/api');
const app = getApp();
const constant = require('../../utils/constant');
const storage = require('../../utils/storage')
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // 选项卡头部高度
    tabHeadHeight: 0,
    // search高度
    searchHeight: 0,
    // 选择仓库的高度
    chooseStoreHeight: 0,
    isSideShow: false,
    current: 0,
    // 猜你喜欢分页
    storeListOption:{
      pageNum: 1,  //分页初始值，页号1
      pageSize: 10, //分页初始值，每页20条
      pos_id: 0
    },
    storeListData: [],
    isStoreListNoData: false,
    isStoreListNoMore: false,
    lendOption: {
      pageNum: 1,
      pageSize: 10,
      lend_store_id: -1
    },
    borrowOption: {
      pageNum: 1,
      pageSize: 10,
      borrow_store_id: -1
    },
    isShowListNoData: false,
    isShowListNoMore: false,
    showChooseStore: {},
    showListData: []
  },
   // 返回上一级
   back:function(){
    console.info('sdf')
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  onHide:function(){
    this.setData({
      isSideShow: false
    })
  },
  /**
   * 页面相关事件处理函数--监听用户分享
   */
  onShareAppMessage: function () {
    return {
      title: '熊猫库存',
      path: '/pages/index/index'
    }
  },
  
  onLoad: function () {
    let that = this;
    // 获取选项卡头部高度
    wx.createSelectorQuery().select('#tabHead').boundingClientRect(rect=>{  
      that.setData({
        tabHeadHeight: rect.height
      })
    }).exec(); 
    // 获取搜索框高度
    wx.createSelectorQuery().select('#search').boundingClientRect(rect=>{  
      that.setData({
        searchHeight: rect.height
      })
    }).exec(); 
    // 获取选择仓库的高度
    wx.createSelectorQuery().select('#chooseStore').boundingClientRect(rect=>{  
      that.setData({
        chooseStoreHeight: rect.height
      })
    }).exec();
    this.getUserIdentity()
    this.getStoreList(this.data.storeListOption)
  },
  // 用户身份
  getUserIdentity:function(){
    api.getUserIdentity().then(res => {
      if(res.code == 0){
        storage.setUserIdentity(res.data.data.status)
      }
    })
  },
  // 创建仓库
  createStore(){
    let userIdentity = wx.getStorageSync(storage.USERIDENTIRY)
    if(userIdentity == 0){
      wx.navigateTo({
        url: '/pages/addStore/addStore',
      })
    }
    if(userIdentity == 1){
      wx.showModal({
        title: '提示',
        content: '你还不是商户，请先去认证成为商户？',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/authenBusiness/authenBusiness'
            })
          }
        }
      })
    }
    if(userIdentity == 2){
      wx.showToast({
        title: '你的商户认证还在审核中，请耐心等待...',
        icon: 'none'
      })
    }
  },
  // 仓库列表
  getStoreList(storeListOption){
    let that = this
    if(this.data.storeListData.length > 0){
      if(!this.data.isStoreListNoMore){
        let storeListData = this.data.storeListData
        api.getStoreList(storeListOption).then(res => {
          if(res.data.code == 0){
            if(res.data.data.list.length > 0){
              storeListData = [...storeListData,...res.data.data.list]
              this.setData({
                storeListData
              })
            }else{
              this.setData({
                'isStoreListNoMore': true
              })
            }
          }
        })
      }
    }else{
      api.getStoreList(storeListOption).then(res => {
        if(res.data.data.list.length > 0){
          this.setData({
            storeListData: res.data.data.list,
            'lendOption.lend_store_id': res.data.data.list[0].store_id,
            'showChooseStore': res.data.data.list[0]
          })
          that.getLendListsData(that.data.lendOption)
        }else{
          this.setData({
            isStoreListNoData: true
          })
        }
      })
    }
  },
  // 选项卡
  tabChange(e){
    let tabType = e.currentTarget.dataset.current
    let showChooseStore = this.data.showChooseStore
    if(tabType != this.data.current){
      this.setData({
        current: tabType,
        showListData: [],
        isShowListNoData: false,
        isShowListNoMore: false,
        'lendOption.lend_store_id': showChooseStore.store_id,
        'borrowOption.borrow_store_id': showChooseStore.store_id
      })
      if(tabType == 0){ // 获取借出列表
        this.getLendListsData(this.data.lendOption)
      }else{ // 获取借入列表
        this.getBorrowListsData(this.data.borrowOption)
      }
    }
  },
  // 获取借出列表
  getLendListsData(option){
    let that = this
    if(this.data.showListData.length > 0){
      if(!this.data.isShowListNoMore){
        api.lendLists(this.data.lendOption).then(res => {
          if(res.data.data.list.length > 0){
            let showListData = that.data.showListData
            showListData = [...showListData,...res.data.data.list]
            this.setData({
              showListData
            })
          }else{
            this.setData({
              isShowListNoMore: true
            })
          }
      })
      }
    }else{
      api.lendLists(this.data.lendOption).then(res => {
          if(res.data.data.list.length > 0){
            this.setData({
              showListData: res.data.data.list
            })
          }else{
            this.setData({
              isShowListNoData: true
            })
          }
      })
    }
  },
  // 获取借入列表
  getBorrowListsData(option){
    let that = this
    if(this.data.showListData.length > 0){
      if(!this.data.isShowListNoMore){
        api.borrowLists(this.data.borrowOption).then(res => {
          if(res.data.data.list.length > 0){
            let showListData = that.data.showListData
            showListData = [...showListData,...res.data.data.list]
            this.setData({
              showListData
            })
          }else{
            this.setData({
              isShowListNoMore: true
            })
          }
      })
      }
    }else{
      api.borrowLists(this.data.borrowOption).then(res => {
          if(res.data.data.list.length > 0){
            this.setData({
              showListData: res.data.data.list
            })
          }else{
            this.setData({
              isShowListNoData: true
            })
          }
      })
    }
  },
  // 背景不滚动
  dontMove: function(){
    return;
  },
  // 侧边栏
  storeBtn:function(){
    this.setData({
      isSideShow : true
    })
  },
  // 选择仓库
  chooseStore(e){
    let current = this.data.current
    let item = e.currentTarget.dataset.item
    if(current == 0){
      let lendOption = {
        pageNum: 1,
        pageSize: 10,
        lend_store_id: item.store_id
      }
      this.setData({
        lendOption,
        showChooseStore: {...item},
        isSideShow : false,
        isShowListNoData: false,
        isShowListNoMore: false,
        showListData: []
      })
      this.getLendListsData(this.data.lendOption)
    }else{
      let borrowOption = {
        pageNum: 1,
        pageSize: 10,
        borrow_store_id: item.store_id
      }
      this.setData({
        borrowOption,
        showChooseStore: {...item},
        isSideShow : false,
        isShowListNoData: false,
        isShowListNoMore: false,
        showListData: []
      })
      this.getBorrowListsData(this.data.borrowOption)
    }
    
    console.info(item)
  },
  // 关闭侧边栏
  closeSide:function(){
    this.setData({
      isSideShow : false
    })
  },
  // 扫一扫
  scan:function(){
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
      }
    })
  },
  // 跳转到添加仓库
  jumpToAddStore:function(){
    wx.navigateTo({
      url: '/pages/addStore/addStore',
    })
  },
  // 跳转到仓库设置
  jumpToStoreSet:function(e){
    wx.reLaunch({
      url: `/pages/setStore/setStore?id=${e.currentTarget.dataset.id}`,
    })
  },
  // 跳转到仓库列表设置
  jumpToSetGoods:function(){
    wx.navigateTo({
      url: '/pages/setOtherStore/setOtherStore',
    })
  },
  // 跳转到货品列表
  jumpToGoodsList:function(e){
    wx.reLaunch({
      url: '/pages/goodsList/goodsList',
    })
  }
})

